<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>添加角色</title>
    <th:block th:replace="componet::reference"></th:block>
</head>
<body>
<div class="layui-row" style="margin-top: 30px">
    <div style="text-align: center;">
        <div class="layui-inline">
            <form class="layui-form layui-form-pane" lay-filter="myForm">
                <div class="layui-form-item">
                    <label for="roleName" class="layui-form-label">角色名称</label>
                    <div class="layui-input-inline">
                        <input type="text" id="roleName" name="roleName"
                               class="layui-input"></div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">
                        角色状态</label>
                    <div class="layui-input-inline">
                        <input type="radio" name="isStart" value="0" title="禁用" class="layui-input">
                        <input type="radio" name="isStart" value="1" title="启用" class="layui-input">
                    </div>
                </div>


                <div class="layui-form-item">
                    <button class="layui-btn" lay-filter="save" lay-submit="">添加</button>
                    <button class="layui-btn" lay-filter="fail" lay-submit="">返回</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'layer'],
        function () {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;

            form.val("myForm", {
                "sex": "[[${session.user.sex}]]"
            });

            //监听返回
            form.on('submit(fail)', function () {
                // 获得frame索引
                var index = parent.layer.getFrameIndex(window.name);
                console.log(window.name);
                // 关闭当前frame
                parent.layer.close(index);


                xadmin.close();
            });


            //监听提交
            form.on('submit(save)',
                function (data) {

                    //发异步，把数据提交给java
                    $.post("/role/addOrUpdate", data.field, function (data) {
                        if (data) {
                            parent.layer.msg("添加成功", {icon: 1});
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            // 关闭当前frame
                            parent.layer.close(index);
                            //重新加载父级页面的表格
                            parent.layui.table.reload('test');
                            xadmin.close();
                        } else {
                            layer.msg("添加失败", {icon: 2});
                        }
                    });

                    return false;
                });

        });

</script>
</body>
</html>